package org.dromara.learning_xq.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.learning_xq.entity.AsChapterContent;
import org.dromara.learning_xq.entity.AsQuestionCondition;
import org.dromara.learning_xq.mapper.AsQuestionConditionMapper;
import org.dromara.learning_xq.service.AsQuestionConditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "题库条件")
@RequestMapping("/asQuestionCondition")
public class AsQuestionConditionController {


    @Autowired
    private AsQuestionConditionService asQuestionConditionService;


    @PostMapping("increaseAsFormativeTest")
    @Operation(summary = "添加题库条件")
    public ResponseEntity<Map<String, Object>> increaseAsFormativeTest(@RequestBody AsQuestionCondition asQuestionCondition) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 调用 service 层的方法进行添加操作
            List<AsQuestionCondition> asQuestionConditions = asQuestionConditionService.increaseAsFormativeTest(asQuestionCondition);

            if (asQuestionConditions != null && !asQuestionConditions.isEmpty()) {
                response.put("message", "添加成功");
                response.put("status", "success");
                response.put("data", asQuestionConditions);
                return ResponseEntity.ok(response);
            } else {
                response.put("message", "添加失败");
                response.put("status", "fail");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
            }
        } catch (Exception e) {
            response.put("message", "添加过程中发生错误: " + e.getMessage());
            response.put("status", "error");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @GetMapping("/selectAsChapter")
    @Operation(summary = "查询章程")
    public AsQuestionCondition getTree() {
        return asQuestionConditionService.selectAsChapterContentDtoList();
    }


    @PostMapping("updateAsQuestionFormativeTest")
    @Operation(summary = "修改题库条件")
    public ResponseEntity<Map<String, Object>> updateAsQuestionConditions(@RequestBody AsQuestionCondition asQuestionCondition) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 调用 service 层的方法进行添加操作
            List<AsQuestionCondition> asQuestionConditions = asQuestionConditionService.updateAsQuestionConditions(asQuestionCondition);

            if (asQuestionConditions != null && !asQuestionConditions.isEmpty()) {
                response.put("message", "修改成功");
                response.put("status", "success");
                response.put("data", asQuestionConditions);
                return ResponseEntity.ok(response);
            } else {
                response.put("message", "修改失败");
                response.put("status", "fail");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
            }
        } catch (Exception e) {
            response.put("message", "修改过程中发生错误: " + e.getMessage());
            response.put("status", "error");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }


} 
